<?php

namespace App\Task\Update;

use App\Model\CloseFriend;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 每天定时修改亲密度
 * @Crontab(name="UpdateCloseTask", rule="10 5 * * *", callback="execute", memo="修改亲密值", singleton=false)
 */
class UpdateCloseTask
{
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        $offsetValue = 1;
        $where = [
            ['consume_ts', '<', strtotime(date('Y-m-d')) - 24 * 3600],
            ['consume_ts', '>', 0],
            ['close_value', '>', $offsetValue],
            ['flag', 1]
        ];
        $records = CloseFriend::where($where)->get()->toArraty();
        if (count($records) == 0) return;
        foreach ($records as $record) {
            log_info(__CLASS__ . "=={$record->id}==old:{$record->close_value}");
            //亲密值
            $record->decrement('close_value', $offsetValue);
        }
    }
}
